<template>
<div>
<template>


<!-- 添加管理员隐藏端口 -->
<el-dialog title="添加管理员" :visible.sync="dialogFormVisible">
  <el-form :model="admin">
    <el-form-item label="管理员姓名" label-width="120px">
      <el-input v-model="admin.username" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label="管理员密码" label-width="120px">
      <el-input  v-model="admin.password" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="管理员邮箱" label-width="120px">
      <el-input v-model="admin.email" autocomplete="off"></el-input>
    </el-form-item>
      <el-form-item label="管理员微信" label-width="120px">
      <el-input v-model="admin.appId" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label-width="200px">
      <el-button size="small"  type="primary" @click="ensureAddAdmin()">添加</el-button>
      <el-button size="small"  type="primary" @click="cancelAddAdmin()">取消</el-button>
    </el-form-item>
  </el-form>
  </el-dialog>

  <!-- 修改管理员隐藏端口 -->
    <el-dialog title="修改管理员" :visible.sync="updateDialogFormVisible">
  <el-form :model="uAdmin">
    <el-form-item label="管理员姓名" label-width="120px">
      <el-input v-model="uAdmin.username" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label="管理员密码" label-width="120px">
      <el-input type="password" v-model="uAdmin.password" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="管理员邮箱" label-width="120px">
      <el-input  v-model="uAdmin.email" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="管理员微信" label-width="120px">
      <el-input  v-model="uAdmin.appId" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label-width="200px">
      <el-button size="small"  type="primary" @click="ensureUpdateAdmin()">修改</el-button>
      <el-button size="small"  type="primary" @click="cancelUpdatedAdmin()">取消</el-button>
    </el-form-item>
  </el-form>
  </el-dialog>

  <!-- 页面主体 -->
   <el-row :gutter="0">

     <!-- 添加管理员按钮 -->
    <el-col :span="3">
      <el-button size="small"  type="primary" @click="addAdmin()">增加管理员</el-button>
    </el-col>

    <!-- 输入框 -->
    <el-col :span="6">
        <el-input
        v-model="adminDTO.username"
        class="w-50 m-2"
        size="small"
        placeholder="请输入姓名"
        @input="search()"
        />
    </el-col>

    <!-- 搜索按钮 -->
    <el-col :span="3">
      <el-button size="small"  type="primary" @click="search()">搜索</el-button>
    </el-col>

     <!-- 导出Excel -->
    <el-col :span="3">
      <el-button size="small"  type="primary" @click="exportExcel()">导出Excel</el-button>
    </el-col>

  </el-row>

  <!-- 表格 -->
  <el-table :data="admins" stripe border height="300" id="mytable">
    <el-table-column prop="id" sortable=true label="ID"/>
    <el-table-column prop="username" sortable=true label="昵称"/>
    <el-table-column prop="email" sortable=true label="邮箱" />
    <el-table-column prop="appId" sortable=true label="微信号" />
        <el-table-column label="操作">
            <template #default="scope">
                <el-button size="small" @click="updateAdmin(scope.row)">
                   修改
                </el-button>
                <el-button size="small" type="danger" @click="deleteAdmin(scope.row.id)">
                   删除
                </el-button>
            </template>
            </el-table-column>
  </el-table>

  <!-- 分页部分 -->
  <el-pagination
    small
    background
    layout="prev, pager, next"
    :page-size="adminDTO.capacity"
    :current-page.sync="adminDTO.index"
    @current-change="changeCurrent"
    :total="count"
    class="mt-4"
  />
</template>

</div>
</template>
<script>
export default {
  name: 'Home',
  data () {
    return {
      // 当前登录的用户角色
      role: '管理员',
      // 表格映射对象
      admins: [],
      // 修改的对象
      uAdmin: {
        username: '',
        password: '',
        roleId: '',
        adminNo: ''
      },
      // 表格查询对象
      adminDTO: {
          index: 0,
          capacity: 10,
          username: ''
      },
      // 角色列表
      roles: [],
      // 添加管理员弹出控制
      dialogFormVisible: false,
      // 修改管理员弹出控制
      updateDialogFormVisible: false,
      // 添加管理员实体
      admin: {
          username: '',
          password: '',
          roleId: '',
          adminNo: ''
      },
      // 管理员列表总数
      count: 0,
      // 用户信息
      user: {},
      // 角色信息
      role: ''
    }
  },
  // 页面初始化函数 
  created () {

    // 获得角色信息
    this.role = window.sessionStorage.getItem('role')

    // 获得用户信息
    this.user = JSON.parse(window.sessionStorage.getItem('user'))

    this.$http({
        url: "/admin",
        params: {
            index: this.adminDTO.index,
            capacity: this.adminDTO.capacity,
            username: this.adminDTO.username
        }
    }).then(response => {
       this.admins = response.data
       console.log(this.admins)
    })

  },
  methods: {
    // 改变当前页
    changeCurrent() {
      findAll()
    },
    // 添加管理员
     addAdmin() {
       this.dialogFormVisible=true;
     },
     updateAdmin(data) {
       this.uAdmin=data
       this.updateDialogFormVisible=true
     },
     deleteAdmin(id) {
        this.$http.delete('/admin/' + id).then(response => {
          this.$message({
          message: '删除管理员成功',
          type: 'success'
          });
          this.findAll()
        })
     },
     ensureAddAdmin() {
      this.$http.post("/admin", this.admin).then(response => {
        this.$message({
          message: '添加管理员成功',
          type: 'success'
        });
        this.clearAdmin()
        this.findAll()
      })
    },
    // 取消添加管理员
    cancelAddAdmin() {
      this.clearAdmin()
    },
    ensureUpdateAdmin() {
      this.$http.put('/admin', this.uAdmin).then(response => {
        this.$message({
          message: '修改管理员成功',
          type: 'success'
        });   
        this.updateDialogFormVisible=false
        this.findAll()
      })
    },
    cancelUpdatedAdmin() {
      this.updateDialogFormVisible=false
    },
    // 搜索对象，并映射
    search() {
      this.findAll()
    },
    // 查询所有，并映射
    findAll() {
    this.$http({
        url: "/admin",
        params: {
            index: this.adminDTO.index,
            capacity: this.adminDTO.capacity,
            username: this.adminDTO.username
        }
    }).then(response => {
       this.admins = response.data
       console.log(this.admins)
    })
    },
    // 清除管理员，并关闭窗口
    clearAdmin() {
        this.admin.username=''
        this.admin.password=''
        this.admin.roleId=''
        this.admin.adminNo=''
        this.dialogFormVisible=false
     },

    // 导出Excel
    exportExcel () {
        var xlsxParam = { raw: true } 
        var wb = this.$xlsx.utils.table_to_book(document.querySelector('#mytable'), xlsxParam)
        /* get binary string as output */
        var wbout = this.$xlsx.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
        try {
            this.$filesaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), this.user.username + '导出管理员信息表.xlsx')
        } catch (e) {
        if (typeof console !== 'undefined') {
            console.log(e, wbout)
        }
        }
        return wbout
    }


  }
}
</script>
<style>

</style>